﻿using AutoMapper;
using B.S.BasicData.Api.Read.Application.Command.User;
using B.S.BasicData.Api.Read.DTOS;
using B.S.BasicData.Domian.Entitys;
using B.S.BasicData.ErrorCode;
using B.S.BasicData.Infastructure.Interface;
using MediatR;

namespace B.S.BasicData.Api.Read.Application.Handler.User
{
    public class GetProvinceHandler : IRequestHandler<GetProvinceCommand, ApiResult<List<ProvinceDto>>>
    {
        private readonly IBaseRepository<ProvinceModel> _provinceRepository;
        private readonly IMapper _mapper;

        public GetProvinceHandler(IBaseRepository<ProvinceModel> provinceRepository, IMapper mapper)
        {
            _provinceRepository = provinceRepository;
            _mapper = mapper;
        }

        /// <summary>
        /// 获取省份下拉框
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public Task<ApiResult<List<ProvinceDto>>> Handle(GetProvinceCommand request, CancellationToken cancellationToken)
        {
            ApiResult<List<ProvinceDto>>result =new ApiResult<List<ProvinceDto>>();

            var provice=_provinceRepository.GetAll().Where(x=>x.IsDelete==false).ToList();

            var model =_mapper.Map<List<ProvinceDto>>(provice);
            result.Code=StatusEnum.OK;
            result.Data = model;
            result.Message = "获取列表成功";
            return Task.FromResult(result); 
        }
    }
}
